package com.jasper.entertaining.controller;

import com.jasper.entertaining.entity.User;
import com.jasper.entertaining.service.UserService;
import com.jasper.entertaining.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private UserService userService;

    /**
     * home页
     * @return
     */
    @GetMapping("/homePage")
    public ModelAndView homePage(){
        return new ModelAndView("homePage");
    }

    /**
     * 登录页
     * @return
     */
    @GetMapping("/registerPage")
    public ModelAndView registerPage(){
        return new ModelAndView("registerPage");
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public ModelAndView  register(User user){
        User user1 = userService.selectUserIsExist(user);
        if(!StringUtils.isEmpty(user1)){
            return new ModelAndView("registerPage");
        }
        user.setUserName(UserUtil.generateDefaultUserName());
        user.setCryptographPassword("35435435");
        user.setUserType(0);
        user.setUserStatus(0);
        int id = userService.register(user);
        return new ModelAndView("redirect:loginPage");
    }

    /**
     * 登录页
     * @return
     */
    @GetMapping("/loginPage")
    public ModelAndView loginPage(){
        return new ModelAndView("loginPage");
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public User login(User user){
        User user1 = userService.login(user);
        return user1;
    }

}
